﻿using System;

namespace Patterns.Singleton {
    public static class AbstractExample {

        public static void Run() {
            // Constructor is protected -- cannot use new
            Singleton s1 = Singleton.Instance();
            Singleton s2 = Singleton.Instance();

            if (s1 == s2)
                Console.WriteLine("Objects are the same instance");
        }
    }

    // "Singleton"
    class Singleton {
        private static Singleton instance;

        // Note: Constructor is 'protected'
        protected Singleton() {
        }

        public static Singleton Instance() {
            // Use 'Lazy initialization'
            if (instance == null) {
                instance = new Singleton();
            }

            return instance;
        }
    }
}
